package com.hhp.hhptt.controller.controllerjinpeng;
import com.hhp.hhptt.common.Result;
import com.hhp.hhptt.entity.Navigation;
import com.hhp.hhptt.entity.SwiperImage;
import com.hhp.hhptt.utils.commonUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/index")
public class NavigationController {
    static HashMap<String, String> objectObjectHashMap = new HashMap<>();
static {
    //存储固定数据

    objectObjectHashMap.put("navigator","1-地图导览-map");
    objectObjectHashMap.put("calendar","2-教学校历-calendar");
    objectObjectHashMap.put("telephone","3-校通讯录-address");
    objectObjectHashMap.put("links","4-官方链接-link");
    objectObjectHashMap.put("teachers","5-优秀师资-teacher");
    objectObjectHashMap.put("propaganda","6-报名咨询-consult");
    objectObjectHashMap.put("lost","7-失物招领-lostAndFind");
    objectObjectHashMap.put("activities","8-最新活动-latestAct");
    objectObjectHashMap.put("second_hand","9-二手交易-secondHand");
    objectObjectHashMap.put("report","10-投诉建议-advice");
    objectObjectHashMap.put("1","1");
    objectObjectHashMap.put("2","2");
    objectObjectHashMap.put("3","3");
    objectObjectHashMap.put("4","4");
    objectObjectHashMap.put("5","5");

}

    @GetMapping("/navigation")
    public Result getNavigationIcon() throws MalformedURLException, URISyntaxException {
//        String fileAbsolutePath = commonUtils.getFileAbsolutePath("menu");
        String fileAbsolutePath=commonUtils.getFileAbsolutePathDeep("digtalcampus","navigator","menu","");
        List<Navigation> objectsMap = new ArrayList<>();
        File folder = new File(fileAbsolutePath);
        // 检查文件夹是否存在且是目录
        if (folder.exists() && folder.isDirectory()) {
            // 获取文件夹中的所有文件和子文件夹
            File[] files = folder.listFiles();

            // 遍历文件和子文件夹
            if (files != null) {
                for (File file : files) {
                    String splitString=file.getName();
                    String[] getOriginalName = splitString.split("\\.");
                    if (objectObjectHashMap.containsKey(getOriginalName[0])){
                        String iconUrl = ServletUriComponentsBuilder.fromCurrentContextPath()
                                .path("/images/")
                                .path(file.getName())
                                .toUriString();
                        String incoName=objectObjectHashMap.get(getOriginalName[0]);
                        Navigation navigation = new Navigation();
                        String[] split = incoName.split("-");
                        navigation.setId(split[0]);
                        navigation.setName(split[1]);
                        navigation.setIcon(iconUrl);
                        navigation.setPage(split[2]);
                        objectsMap.add(navigation);
                    }

                }
            } else {
                System.out.println("该文件夹是空的或者无法访问文件夹内容。");
            }
        } else {
            System.out.println("指定路径不是一个有效的文件夹。");
        }

        List<Navigation> sortedNavigation = objectsMap.stream()
                .sorted(Comparator.comparingInt(nav -> Integer.parseInt(nav.getId())))
                .collect(Collectors.toList());
        return Result.success("请求成功，返回导航数据",sortedNavigation);
    }

    @GetMapping("/swiper_image")
    public Result getSwiperImage() throws MalformedURLException, URISyntaxException {
//        String fileAbsolutePath = commonUtils.getFileAbsolutePath("swiperImage");
        String fileAbsolutePath=commonUtils.getFileAbsolutePathDeep("digtalcampus","swiper","swiperImage","");
        List<SwiperImage> objectsMap = new ArrayList<>();
        File folder = new File(fileAbsolutePath);
        // 检查文件夹是否存在且是目录
        if (folder.exists() && folder.isDirectory()) {
            // 获取文件夹中的所有文件和子文件夹
            File[] files = folder.listFiles();

            // 遍历文件和子文件夹
            if (files != null) {
                for (File file : files) {
                    String splitString=file.getName();
                    String[] getOriginalName = splitString.split("\\.");
                    if (objectObjectHashMap.containsKey(getOriginalName[0])){
                        String iconUrl = ServletUriComponentsBuilder.fromCurrentContextPath()
                                .path("/images/")
                                .path(file.getName())
                                .toUriString();
                        String incoName=objectObjectHashMap.get(getOriginalName[0]);
                        SwiperImage swiperImage = new SwiperImage();
                        String[] split = incoName.split("-");
                        swiperImage.setId(split[0]);
                        swiperImage.setImg(iconUrl);
                        objectsMap.add(swiperImage);
                    }

                }
            } else {
                System.out.println("该文件夹是空的或者无法访问文件夹内容。");
            }
        } else {
            System.out.println("指定路径不是一个有效的文件夹。");
        }
        return Result.success("请求成功，返回轮播图数据",objectsMap);
    }
}
